package StacksAndQueues;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Bjarke
 * Date: 06-11-13
 * Time: 20:21
 * To change this template use File | Settings | File Templates.
 */
public class TheStack {

    private String stackArray[];
    private int stackSize;
    private int topOfStack = -1;

    TheStack(int size){
        size = this.stackSize;
        stackArray = new String[size];
        Arrays.fill(stackArray, "-1");
    }

    public void push(String input){

        if (topOfStack + 1 < stackSize){
            topOfStack++;
            stackArray[topOfStack] = input;
        }else{
            System.out.println("Not enough room");
        }

        displayTheStack();

        System.out.println("PUSH " + input + " Was added to the Stack");
    }

    public String pop(){
        if (topOfStack >= 0){
            displayTheStack();
            System.out.println("Push" + stackArray[topOfStack] + " was removed from the stack\n" );
            stackArray[topOfStack] = "-1";
            return stackArray[topOfStack--];
        }else{
            displayTheStack();
            System.out.println("Sorry but the stack is empty");
            return "-1";
        }
    }

    public String peek(){
        displayTheStack();
        System.out.println("Peek " + stackArray[topOfStack] + " Is at the top of the stack");
        return stackArray[topOfStack];
    }

    public void displayTheStack(){

        for(int n = 0; n < 61; n++)System.out.print("-");

        System.out.println();

        for(int n = 0; n < stackSize; n++){

            System.out.format("| %2s "+ " ", n);

        }

        System.out.println("|");

        for(int n = 0; n < 61; n++)System.out.print("-");

        System.out.println();

        for(int n = 0; n < stackSize; n++){



            if(stackArray[n].equals("-1")) System.out.print("|     ");

            else System.out.print(String.format("| %2s "+ " ", stackArray[n]));

        }

        System.out.println("|");

        for(int n = 0; n < 61; n++)System.out.print("-");

        System.out.println();

    }

    public static void main(String args[]){

        TheStack theStack = new TheStack(10);
        theStack.push("10");
        theStack.push("15");
        theStack.peek();
        theStack.pop();
        theStack.displayTheStack();


    }


}
